//
//  YNWKWebViewController.m
//  YNDemo
//
//  Created by 胡锦涛 on 2020/11/18.
//

#import "YNWKWebViewController.h"
#import <WebKit/WebKit.h>
#import <YNLibBase/HKLibBaseHeader.h>
#import <YNLibCommon/HKPrefixHeader.h>
// 架构模式
#import "BifrostHeader.h"
#import "WebViewModuleService.h"

@interface YNWKWebViewController ()<WKNavigationDelegate>
@property (nonatomic, strong) UIButton *backBtn;
@property (strong , nonatomic) WKWebView *webView;
@end

@implementation YNWKWebViewController
+ (void)load {
    [Bifrost bindURL:kRouteWKWebViewPage
           toHandler:^id _Nullable(NSDictionary * _Nullable parameters) {
        return [YNWKWebViewController new];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
}
-(UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return UIStatusBarStyleDefault;
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)setupUI {
    self.view.backgroundColor = UIColorWhite;
    self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-HK_NAVBAR_HEIGHT)];
    self.webView.navigationDelegate = self;
    HK_AdjustsContentInsets(_webView.scrollView);
    [self.view addSubview:_webView];
    if (self.webUrl) {
        NSURLRequest * request = [NSURLRequest requestWithURL:_webUrl];
        [_webView loadRequest:request];
    }else {
        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]]];
        [_webView loadRequest:request];
    }
    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
}
#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context
{
    if (object == self.webView) {
        
        if ([keyPath isEqualToString:@"title"]) {
            if (self.webTitle && [self.webTitle qmui_trim].length > 0) {
                self.navigationItem.title = self.webTitle;
            }else {
                self.navigationItem.title = change[@"new"];
            }
            
        }
    }
    
}
- (void)setWebUrl:(NSURL *)webUrl {
    _webUrl = webUrl;
}
-(void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"title" context:nil];
}
@end
